package cn.org.zx.common.dispartPage;

import cn.org.zx.db.dbconnection.DBProperties;

public class CreatePageSQL {
	private String strSelectSQL;
	private String strOrder;
	private int pageSize;
	private int pageNo;
	
	//pageSize://每页显示多少条
	//pageNo ////第几页
	public CreatePageSQL(String strSelectSQL,String strOrder,int pageSize,int pageNo)
	{
		this.strSelectSQL = strSelectSQL;
		this.strOrder=strOrder; 
		this.pageSize = pageSize;
		this.pageNo = pageNo;

	}
	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		String strDBType = DBProperties.getDBType();
		if(strDBType.equals("sqlserver")){
			String tempstr="select ROW_NUMBER() OVER(ORDER BY "+strOrder+") AS rownum,";
			String strSelect=strSelectSQL.replaceFirst("select",tempstr);
			sb.append("select * from(");
			sb.append(strSelect);
			sb.append(") as tempdb where rownum between ");
			sb.append(String.valueOf(pageSize*(pageNo-1)+1));
			sb.append(" and ");
			sb.append(String.valueOf(pageSize*(pageNo)));
		}
		else if(strDBType.equals("oracle")){
			StringBuffer strSelect = new StringBuffer();			
			strSelect.append("select rownum as rownumber,t1.* from (");
			strSelect.append(strSelectSQL);
			strSelect.append(" order by ");
			strSelect.append(strOrder);
			strSelect.append(") t1 ");
			
			sb.append("select * from(");
			sb.append(strSelect);
			sb.append(") tempdb where rownumber between ");
			sb.append(String.valueOf(pageSize*(pageNo-1)+1));
			sb.append(" and ");
			sb.append(String.valueOf(pageSize*(pageNo)));
		}
		else if(strDBType.equals("mysql")){
			sb.append("select * from(");
			sb.append(strSelectSQL);
			sb.append(") as tempdb order by "+strOrder+" limit ");
			sb.append(String.valueOf(pageSize*(pageNo-1)));
			sb.append(" , ");
			sb.append(String.valueOf(pageSize*(pageNo)));
		}		
		
		return sb.toString();
	}
	
	public static void main(String args[])
	{	

    }
}
